const KoaBouncer = require('koa-bouncer')
const {GETGOODS,ADDGOODS,UPDATEGOODS,DELGOODS,SEARCHGOODS} = require('../service/goods.service')
module.exports = {
    // 获取商品
    getGoods: async (ctx, next) => {
        await GETGOODS(ctx, next)
    },
    // 添加商品
    addGoods: async (ctx, next) => {
        try {
            ctx.validateBody('id')
                .required('id必须得传值')
                .isString()
                .trim()
            ctx.validateBody('goodsName')
                .required('goodsName必须得传值')
                .isString()
                .trim()
                ctx.validateBody('goodsItn')
                .required('goodsItn必须得传值')
                .isString()
                .trim()
                ctx.validateBody('goodsPrice')
                .required('goodsPrice必须得传值')
                .isString()
                .trim()
                ctx.validateBody('goodsImg')
                .required('goodsImg必须得传值')
                .isString()
                .trim()
                ctx.validateBody('goodsNum')
                .required('goodsNum必须得传值')
                .isString()
                .trim()
             
                ctx.validateBody('belongToShop')
                .required('belongToShop必须得传值')
                .isString()
                .trim()
                await ADDGOODS(ctx, next)
        } catch (error) {
            if (error instanceof KoaBouncer.ValidationError) {
                ctx.body = {
                    code: 500,
                    msg: error.message
                }
                return
            } else {
                throw error;
            }
        }
 
    },
    // 更新商品
    updateGoods: async (ctx, next) => {
        try {
            ctx.validateBody('id')
                .required('id必须得传值')
                .isString()
                .trim()
            ctx.validateBody('goodsName')
                .required('goodsName必须得传值')
                .isString()
                .trim()
                ctx.validateBody('goodsItn')
                .required('goodsItn必须得传值')
                .isString()
                .trim()
                ctx.validateBody('goodsPrice')
                .required('goodsPrice必须得传值')
                .isString()
                .trim()
                ctx.validateBody('goodsImg')
                .required('goodsImg必须得传值')
                .isString()
                .trim()
                ctx.validateBody('goodsNum')
                .required('goodsNum必须得传值')
                await UPDATEGOODS(ctx, next)
        } catch (error) {
            if (error instanceof KoaBouncer.ValidationError) {
                ctx.body = {
                    code: 500,
                    msg: error.message
                }
                return
            } else {
                throw error;
            }
        }
    },
    // 删除商品
    delGoods: async (ctx, next) => {
        try {
            ctx.validateBody('id')
                .required('id必须得传值')
                .isString()
                .trim()
            await DELGOODS(ctx, next)
        } catch (error) {
            if (error instanceof KoaBouncer.ValidationError) {
                ctx.body = {
                    code: 500,
                    msg: error.message
                }
                return
            } else {
                throw error;
            }
        }
       
    },
    // 搜索商品
    searchGoods: async (ctx, next) => {
        try {
            ctx.validateBody('keyWord')
                .required('keyWord必须得传值')
                .isString()
                .trim()
            ctx.validateBody('belongToShop')
                .required('belongToShop必须得传值')
            await SEARCHGOODS(ctx, next)
        } catch (error) {
            if (error instanceof KoaBouncer.ValidationError) {
                ctx.body = {
                    code: 500,
                    msg: error.message
                }
                return
            } else {
                throw error;
            }
        }
    },
}